# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter


class QiusbkPipeline:

    file = None

    def open_spider(self, spider):
        # 爬虫开始时调用 只会执行一次
        print("开始获取数据......")
        self.file = open('./2.txt', 'w', encoding='utf-8')

    def process_item(self, item, spider):
        # 接收爬虫文件提交的item对象
        # 每接受一次item就会调用一次该方法
        author = item['author']
        text = item['text']
        self.file.write('{}:\n{}\n'.format(author.rstrip(), text.lstrip()))
        return item

    def close_spider(self, spider):
        # 爬虫结束时调用 只会执行一次
        print("获取数据完成！")
        self.file.close()
